package com.opengms.HydroModelsIntegration.Controller;

import com.opengms.HydroModelsIntegration.DTO.ModelServiceMigrationDTO;
//import com.opengms.HydroModelsIntegration.service.DataService;
import com.opengms.HydroModelsIntegration.service.ModelService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * @Author: David.Xiao
 * @Date: 2019/1/3 15:37
 * @Description:
 */

@RestController
@RequestMapping("/webswmm")
public class WebSwmmController {

//    @Autowired
//    DataService dataService;

    @Autowired
    ModelService modelService;

//    @Autowired
//    HttpSession httpSession;

    public WebSwmmController() {

    }

    /**
     *
     * @param uploadFile 上传的文件
     * @return 上传的文件在服务器上的GUID文件名
     */
    @RequestMapping("/uploadFile")
    @ResponseBody
    public String uploadFile(@RequestParam("input-uploadfile") MultipartFile uploadFile)
    {
//        httpSession.setAttribute("userId",modelService.getUserId());
        return modelService.openFile(uploadFile);
    }

    /**
     * 加载带数据的侧边树
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/loadTree",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONArray loadTree(@RequestBody JSONObject jsonObject)
    {
        JSONArray jsonArray = jsonObject.getJSONArray("initTree");
        return modelService.makeTree(jsonArray);
    }

    /**
     *
     * @param fileName 上传的文件在服务器上的GUID文件名
     * @return inp经过解析的json字符串
     */
    @RequestMapping("/parseInputData")
    @ResponseBody
    public String parseInputData(@RequestParam("fileName") String fileName)
    {
        String inpJsonStr = modelService.parseInputData(fileName);

        return inpJsonStr;
    }

    /**
     *
     * @param inpData 用户修改后的inp json字符串
     * @return 输出数据在模型容器上的路径
     */
    @RequestMapping(value = "/runModel",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONArray runModel(@RequestBody JSONObject inpData)
    {

        return modelService.runModel(inpData);
    }


    @RequestMapping(value = "/visualizeOutput",method = RequestMethod.GET)
    public String visualizeOutput()
    {
        return modelService.readUdxData();
    }

    @RequestMapping(value = "/loadOutputSchema",method = RequestMethod.GET)
    public String loadOutputSchema()
    {
        return modelService.readOutputSchema();
    }

    @RequestMapping(value = "/loadInputSchema",method = RequestMethod.GET)
    public String loadInputSchema()
    {
        return modelService.readInputSchema();
    }

    @RequestMapping(value = "/getModelServicesInfo",method = RequestMethod.GET)
    @ResponseBody
    public JSONArray getModelServicesInfo()
    {
//        return null;
       return modelService.getModelServicesInfo(modelService.getModelServiceConfig().getClass());
    }

    @RequestMapping(value = "/setServiceWrapperGuid",method = RequestMethod.POST)
    @ResponseBody
    public String setServiceWrapperGuid(@RequestParam("serviceWrapperId") String serviceWrapperId)
    {
        modelService.setModelServiceWrapperId(serviceWrapperId);
        return "1";
    }

    @PostMapping(value = "/getMigratoryModelServiceInfo")  //相当于@RequestMapping的POST方法
    @ResponseBody
    public JSONArray getMigratoryModelServiceInfo()
    {
        return modelService.getModelServicesInfo(modelService.getMigratoryModelServiceConfig().getClass());
    }

    @PostMapping(value = "/setModelServiceMigration")  //相当于@RequestMapping的POST方法
    @ResponseBody
    public String setModelServiceMigration(@RequestBody ModelServiceMigrationDTO migrationDTO)
    {
        //TODO 不再将配置文件拷贝到私人文件夹，而在内存中对其进行操作
        return modelService.setModelServiceMigration(migrationDTO);
    }

//    private String getUserId()
//    {
//        return (String) httpSession.getAttribute("userId");
//    }



}
